package datagen;

import com.alibaba.fastjson.JSON;
import com.gmall.data.pojo.LogBean;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.HashMap;
import java.util.Properties;

/**
 * 模拟用户行为数据写入kafka
 */
public class GenLogData {
    public static void main(String[] args) throws Exception {

        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "localhost:9092");
        props.put("acks", "all");// acks=0 配置适用于实现非常高的吞吐量 , acks=all 这是最安全的模式
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String, String> producer = new KafkaProducer<>(props);

        String[] events = {"pageView", "addCart", "collect", "thumbUp", "share", "search", "adShow", "adClick", "fetchCoupon", "useCoupon", "register"};

        for (int i = 0; i < 10; i++) {
            new Thread(
                    new Runnable() {
                        @Override
                        public void run() {
                            while (true) {
                                LogBean bean = new LogBean();
                                // 决定人数，3个就可能有1000人
                                String account = StringUtils.leftPad(RandomStringUtils.randomNumeric(2), 4, "0");
                                bean.setAccount(account);
                                String eventId = events[RandomUtils.nextInt(0, events.length)];
                                bean.setEventId(eventId);
                                bean.setTimeStamp(System.currentTimeMillis());
                                bean.setAppId("cn.do");
                                bean.setAppVersion("3.0");
                                bean.setCarrier("中国移动");
                                bean.setDeviceId(account);
                                bean.setDeviceType("mi10");
                                bean.setIp("152.33.68.90");
                                bean.setLatitude(56.29385792);
                                bean.setLongitude(120.329857234);
                                bean.setSessionId("sessionid" + account);
                                bean.setResolution("1024*2048");
                                bean.setReleaseChannel("小米应用");
                                bean.setNetType("wifi");
                                bean.setOsName("android");
                                bean.setOsVersion("9.0");

                                HashMap<String, String> properties = new HashMap<>();
                                // 页面数量可能达到10种
                                properties.put("pageId", RandomUtils.nextInt(1, 11) + "");
                                if (eventId.equals("addCart") || eventId.equals("collect")
                                        || eventId.equals("share") || eventId.equals("thumbUp")) {
                                    properties.put("productId", RandomUtils.nextInt(1, 11) + "");
                                }
                                if (eventId.equals("fetchCoupon") || eventId.equals("useCoupon")) {
                                    properties.put("couponId", RandomUtils.nextInt(1, 11) + "");
                                }
                                if (eventId.equals("adShow") || eventId.equals("adClick")) {
                                    properties.put("adId", RandomUtils.nextInt(1, 11) + "");
                                }

                                if (eventId.equals("search")) {
                                    properties.put("keyword", RandomStringUtils.randomNumeric(1));
                                }

                                bean.setProperties(properties);

                                producer.send(new ProducerRecord<String, String>("ods_app_log", JSON.toJSONString(bean)));

                                try {
                                    // 20ms相当于每秒2k条，相当于2w人同时在线
                                    // 当前配置情况下，完全hold住没有任何反压延迟
                                    // 内存占用约6G（原因，缓存没有清除机制，内存一直在增长）
                                    Thread.sleep(RandomUtils.nextInt(1000, 4000));
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                            }
                        }
                    }
            ).start();

        }
    }
}
